﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Acms.Bll.Tests
{
    /// <summary>
    /// Summary description for SitesTest
    /// </summary>
    [TestClass]
    public class SitesTest : Base
    {

        private void ValidateProperties(TestData.Site expected, Acms.Bll.Site actual)
        {
            Assert.AreEqual(expected.Id, actual.Id);
            Assert.AreEqual(expected.Name, actual.Name);
            Assert.AreEqual(expected.Root, actual.Root);
            Assert.AreEqual(expected.VersionId, (byte[])actual.ConcurencyId);
        }
        
        [TestMethod]
        public void Site_GetById_ReturnsNull_IfIncorrectId()
        {
            Assert.IsNull(Bll.Site.GetById(int.MaxValue));
        }

        [TestMethod]
        public void Site_GetById_ReturnsNotNull_IfCorrectId()
        {
            Assert.IsNotNull(Bll.Site.GetById(_TestSites[0].Id));
        }

        [TestMethod]
        public void Site_GetById_ReturnsCorrectProperties()
        {
            var tmp = Bll.Site.GetById(_TestSites[0].Id);
            
            Assert.IsNotNull(tmp);

            ValidateProperties(_TestSites[0], tmp);
        }


        [TestMethod]
        public void Site_GetByRootUrl_ReturnsNull_WithIncorectPath()
        {
            var tmp = Bll.Site.GetByRootUrl("rubbish");
            Assert.IsNull(tmp);
        }

        [TestMethod]
        public void Site_GetByRootUrl_ReturnsDefaultPath_Populates_Correctly()
        {
            var tmp = Bll.Site.GetByRootUrl(_TestSites[0].Root);
            Assert.IsNotNull(tmp);
            ValidateProperties(_TestSites[0], tmp);
        }

        /*[TestMethod]
        public void Site_GetByPathReturnsNullWithNotDefaultPath()
        {
            Site site = new Bll.SiteFactory(_Context).GetByPath("localhost/testing2");
            Assert.IsNotNull(site);
            Assert.AreEqual(_TestSite.Id, site.Id);
            Assert.AreEqual("Test Site", site.Name);
            Assert.AreEqual("localhost/testing1", site.RootUrl);
            Assert.AreEqual(DateTime.Parse("24/10/1975 12:00:00"), site.Created);
        }*/

        [TestMethod]
        public void SiteGetByPathReturnsNullWithCaseInsesitivePath()
        {
            var tmp = Bll.Site.GetByRootUrl(_TestSites[0].Root.ToUpper());
            Assert.IsNotNull(tmp);
            ValidateProperties(_TestSites[0], tmp);
        }

        [TestMethod]
        public void SiteInsertTest()
        {
            var tmp = Bll.Site.CreateInstance();
            tmp.Name = "Create New Test";
            tmp.Root = "localhost/CreateNewTest";

            tmp.Update();

            Assert.IsTrue(tmp.Id > 0);

            var db = new DBDataContext().Sites.Where(x => x.Id == tmp.Id).Single();
            ValidateProperties(db, tmp);
        }


        [TestMethod]
        public void SiteDeleteTest()
        {
            var tmp = Bll.Site.GetById(_TestSites[0].Id);
            Assert.IsNotNull(tmp);

            tmp.Delete();

            var tmp2 = Bll.Site.GetById(_TestSites[0].Id);
            Assert.IsNull(tmp2);
        }

    }
}
